<?php
class Topic_model extends MY_Model {

	
    function __construct(){
        parent::__construct();
        $this->tbl_name = 'edm_orders';
    }

    function add($data){
    	return $this->save('topic',$data);
    }
    
    function get_by_id($id){
    	$r = $this->row('topic',array('id'=>$id,'status'=>1));
		$r->poster = parent::get_by_id('member',$r->poster_id);		    	
		return $r;
    }
    
    function get_sub($id){
    	
    	$query = $this->db->select('t.*,i.path,i.big_path,m.nname as postername')
    		->from('topic t')
    		->join('member m ','m.id=t.poster_id','left')
    		->where(array('t.status'=>1,'pid'=>$id))
    		->get();
    	
    	return $query->result();	
    }
    
    function get_list_by_page($fromnum,$cond=array()){
    	
    	$where = array();
    	$where['t.status'] = 1;
    	$where['t.type'] = 1;
    	if(!empty($cond['poster_id'])){
    		$where['t.poster_id'] = $cond['poster_id'];
    	}

    	$this->db ->from('topic t')->where($where);
    	$this->count = $this->db->count_all_results();
    	
    	$this->db->from('topic t')
    		->select('t.*')
    		->select('m.nname as postername,m.email as posteremail')
    		->select('m1.id as rposter_id,m1.nname as rpostername,m1.email as rposteremail')
    		->join('member m','m.id=t.poster_id','left')
    		->join('topic t1','t1.id=t.rid','left')
    		->join('member m1','m1.id=t1.poster_id','left')
    		->order_by('t.utime desc')
    		->where($where);
    		
    	$query = $this->db->limit(PAGESIZE,$fromnum)->get();
    		
    	return $query->result();
    }
    
    function get_follow_by_page($fromnum,$loginer_id){
    	
    	$where = array();
    	$where['t.status'] = 1;
    	$where['t.type'] = 1;

    	$this->load->model('member_model');
    	$arr_follow_id = $this->member_model->get_follow_ids($loginer_id);
    	$this->db ->from('topic t')->where($where);
    	$arr_follow_id[] = $loginer_id;
    	$this->db->where_in('t.poster_id',$arr_follow_id);
    	
    	$this->count = $this->db->count_all_results();
    	
    	$this->db->from('topic t')
    		->select('t.*')
    		->select('m.nname as postername,m.email as posteremail')
    		->select('m1.id as rposter_id,m1.nname as rpostername,m1.email as rposteremail')
    		->join('member m','m.id=t.poster_id','left')
    		->join('topic t1','t1.id=t.rid','left')
    		->join('member m1','m1.id=t1.poster_id','left')
    		->order_by('t.utime desc')
    		->where($where);
    		
    	if(!empty($arr_follow_id)){
	    	$this->db->where_in('t.poster_id',$arr_follow_id);
    	}
    	
    	$query = $this->db->limit(PAGESIZE,$fromnum)->get();
    		
    	return $query->result();
    }
    
    function get_latest($page){
    	
    	$where = array();
    	$where['t.status'] = 1;
    	$where['t.type'] = 1;

    	$this->db->from('topic t')
    		->select('t.*')
    		->select('m.nname as postername,m.email as posteremail')
    		->select('m1.id as rposter_id,m1.nname as rpostername,m1.email as rposteremail')
    		->join('member m','m.id=t.poster_id','left')
    		->join('topic t1','t1.id=t.rid','left')
    		->join('member m1','m1.id=t1.poster_id','left')
    		->order_by('t.utime desc')
    		->where($where);

    	$query = $this->db->limit(PAGESIZE,($page-1)*PAGESIZE)->get();
    		
    	return $query->result();
    }
    
    function get_recmd_by_page($fromnum,$loginer_id){
    	
    	$where = array();
    	$where['r.to_id'] = $loginer_id;
    	
    	//数量
    	$this->db ->from('recmd r')->where($where)->where_in('r.del',array(0,2));
    	$this->count = $this->db->count_all_results();
    	
    	$this->db ->from('recmd r')
    		->select('r.*')
    		->select('t.*')
    		->select('t1.music_id as r_music_id,t1.music_name as r_music_name')
    		->select('m.nname as postername,m.email as posteremail')
    		->join('topic t','t.id=r.topic_id','left')
    		->join('member m','m.id=t.poster_id','left')
    		->join('topic t1','t1.id=r.reason_id','left')
    		->order_by('t.utime desc')
    		->where_in('r.del',array(0,2))
    		->where($where);
    	
    	$query = $this->db->limit(PAGESIZE,$fromnum)->get();
    		
    	return $query->result();
    }
    
    function get_recmd_meto_by_page($fromnum,$loginer_id){
    	
    	$where = array();
    	$where['r.from_id'] = $loginer_id;
        $where['r.del <> '] = 2;
    	//数量
    	$this->db ->from('recmd r')->where($where)->where_in('r.del',array(0,1));
    	$this->count = $this->db->count_all_results();
    	
    	$this->db ->from('recmd r')
    		->select('r.*')
    		->select('t.*')
    		->select('t1.music_id as r_music_id,t1.music_name as r_music_name')
    		->select('m.nname as postername,m.email as posteremail')
    		->join('topic t','t.id=r.topic_id','left')
    		->join('member m','m.id=r.to_id','left')
    		->join('topic t1','t1.id=r.reason_id','left')
    		->order_by('t.utime desc')
    		->where_in('r.del',array(0,1))
    		->where($where);
    	
    	$query = $this->db->limit(PAGESIZE,$fromnum)->get();
    	$list = $query->result();	
    	return $list;
    }
    
    function get_favo_by_page($fromnum,$loginer_id){
    	
    	$where = array();
    	$where['t.status'] = 1;

    	$this->load->model('favo_model');
    	$arr_id = $this->favo_model->get_favo_ids($loginer_id);
    	$arr_id[] = 0;
    	$this->db ->from('topic t')->where($where);
   	    $this->db->where_in('t.id',$arr_id);
    	$this->count = $this->db->count_all_results();
    	
    	$this->db->from('topic t')
    		->select('t.*')
    		->select('m.nname as postername,m.email as posteremail')
    		->select('m1.id as rposter_id,m1.nname as rpostername,m1.email as rposteremail')
    		->join('member m','m.id=t.poster_id','left')
    		->join('topic t1','t1.id=t.rid','left')
    		->join('member m1','m1.id=t1.poster_id','left')
    		->order_by('t.utime desc')
    		->where($where);
    		
    	$this->db->where_in('t.id',$arr_id);
    	$query = $this->db->limit(PAGESIZE,$fromnum)->get();
    		
    	return $query->result();
    }
    
	public function list_favo_status($list,$loginer_id = 0){
	    $arr_id = array();
	    $arr_id[] = 0;
	    foreach ($list as $vv){
	        $arr_id[] = $vv->id;
	    }
	    $query = $this->db->from($this->tbl->favo)
	        ->where(array('member_id'=>$loginer_id))
	        ->where_in('tid',$arr_id)
	        ->get();
	        
	    $list_favo = $query->result();
        $arr_tid = array();
        foreach ($list_favo as $k=>$v){
            $arr_tid[$v->tid] = $v->tid;
        }	    
	    
	    foreach ($list as $k=>$v){
	        $status = 'normal';
    	    if($loginer_id>0){
    	        if($loginer_id == $v->poster_id){
        	        $status = 'unfavo';
    	        }else{
                    if(in_array($v->id, $arr_tid)){
                        $status = 'favoed';
                    }            	    
    	        } 
    	    }
	        
	        $list[$k]->favo_status = $status;
	    }
	    return $list;
	}
	
	function get_rs_by_kws($kws,$page=1){
	    
		$this->load->library('snoopy');		

		//用户中心登录验证
		$this->snoopy->fetch('http://www.xiami.com/app/nineteen/search/key/'.$kws.'/page/'.$page.'?random='.mktime());
		$rs = $this->snoopy->results;
        return json_decode($rs);
        
        
	}
    
}